/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import DAO.DanhMuc_DAO;
import DTO.DanhMuc_DTO;
import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 *
 * @author phihai
 */
public class Servlet_DanhMuc extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    DanhMuc_DAO spda = new DanhMuc_DAO();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            String action = request.getParameter("action");
            String appPath = request.getServletContext().getRealPath("../../");                          
            
            if (action.equals("ThemSP")) 
            {  
                 
                
//                Part filePart = request.getPart("file");
//                String fileName = getFilename(filePart);
//                InputStream is = filePart.getInputStream();  
//                
//                String savePath = appPath + File.separator + "web/images/"+fileName;
////                try 
//                {
//                    OutputStream os = new FileOutputStream(new File(savePath)); 
//                    try
//                    {                      
//                        byte[] buffer = new byte[4096];  
//                        for (int n; (n = is.read(buffer)) != -1; )
//                        {
//                            os.write(buffer, 0, n);
//                        }  
//                    }
//                    finally { os.close(); } 
//                }
//                finally { is.close(); }                                                                                      
                try
                {
                    String tenSP = request.getParameter("txtTenDanhMuc");
                    DanhMuc_DTO sp = new DanhMuc_DTO();                   
                    sp.setTenDanhMuc(tenSP);                  
                    spda.ThemDanhMuc(sp);
                    response.sendRedirect("ad_category.jsp");
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                } 
            }
            else if(action.equals("XoaSP"))
            {               
                try
                {
                    int ID = Integer.parseInt(request.getParameter("ID"));
                    
                    spda.XoaDanhMuc(ID);
                    response.sendRedirect("ad_category.jsp");
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }                
            }
            else if(action.equals("TimKiemTenSP"))
            {                               
                String tenSP = request.getParameter("txtten");
                request.getSession().setAttribute("timkiem", tenSP);                   
                response.sendRedirect("ad_category.jsp");                           
            }
            else if(action.equals("EditDM"))
            {                               
                int ID = Integer.parseInt(request.getParameter("ID"));
                DanhMuc_DTO sp = spda.LaySPTheoID(ID);
                request.getSession().setAttribute("obj", sp);                   
                response.sendRedirect("ad_category_Sua.jsp");                           
            }
            else if(action.equals("UpdateDM"))
            {  
                String MaSP = request.getParameter("IDDM");
                String tenSP = request.getParameter("txtTenDanhMuc");                                                                                                
                try
                {
                    DanhMuc_DTO sp = new DanhMuc_DTO(); 
                    sp.setIDDanhMuc(Integer.parseInt(MaSP));
                    sp.setTenDanhMuc(tenSP);                   
                    spda.CapNhatDanhMuc(sp);
                    response.sendRedirect("ad_category.jsp");
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }                     
            }    
        } 
        catch(Exception ex)
        {
            ex.printStackTrace();
        } 
    }
    private static String getFilename(Part part) {
    for (String cd : part.getHeader("content-disposition").split(";")) {
        if (cd.trim().startsWith("filename")) {
            String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
        }
    }
    return null;
}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
